package top.i51213.fastaction;

import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import top.i51213.fastaction.R;
import top.i51213.fastaction.action.Action;
import top.i51213.fastaction.action.BaseActonComponent;
import top.i51213.fastaction.util.Util;

public class ActionActiveAdapter extends RecyclerView.Adapter<ActionActiveAdapter.ViewHolder> {
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public TextView tv_title;
        public TextView tv_info;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.recycler_item_image);
            tv_title = itemView.findViewById(R.id.recycler_item_title);
            tv_info = itemView.findViewById(R.id.recycler_item_info);
        }
    }

    private final List<Action> actionList = new ArrayList<>();

    public ActionActiveAdapter(Collection<? extends Action> actionList) {
        this.actionList.addAll(actionList);
    }

    public void addActionData(Action action) {
        int index = this.actionList.size();
        this.actionList.add(action);
        this.notifyItemInserted(index);
    }

    public void addActionData(Collection<? extends Action> actionList) {
        if (actionList.size() > 0) {
            int index = this.actionList.size();
            this.actionList.addAll(actionList);
            this.notifyItemRangeInserted(index, actionList.size());
        }
    }

    public List<Action> getActionList() {
        return actionList;
    }

    public void removeData(int position) {
        getActionList().remove(position);
    }

    @NonNull
    @Override
    public ActionActiveAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_recycler_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ActionActiveAdapter.ViewHolder holder, int position) {
        Action action = actionList.get(position);
        holder.image.setImageResource(action.getImageResId());
        holder.tv_title.setText(action.getTitle());
        holder.tv_info.setText(action.getInfo());
        holder.itemView.setOnClickListener(new OnLick(action, this, position));
    }

    @Override
    public int getItemCount() {
        return actionList.size();
    }

    interface ItemDataChange {
        void change();
    }

    static class OnLick implements View.OnClickListener {
        final Action action;
        final WeakReference<ActionActiveAdapter> adapter;
        final int position;
        long last = System.currentTimeMillis();

        public OnLick(Action action, ActionActiveAdapter adapter, int position) {
            this.action = action;
            this.adapter = new WeakReference<>(adapter);
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            long time = System.currentTimeMillis();
            long offset = time - last;
            last = time;
            if (offset < 500) {
                //Toast.makeText(v.getContext(), action.getTitle(), Toast.LENGTH_SHORT).show();
                BaseActonComponent actonComponent = (BaseActonComponent) action;
                View editView = actonComponent.getEditView();
                if (editView != null) {
                    new AlertDialog.Builder(v.getContext())
                            .setCancelable(false)
                            .setView(editView)
                            .setOnDismissListener(dialog -> {
                                ((ViewGroup) editView.getParent()).removeView(editView);
                                actonComponent.editViewDismissListener();
                                ActionActiveAdapter adapter=this.adapter.get();
                                if(adapter!=null){
                                    adapter.notifyItemChanged(position);
                                }
                            }).setPositiveButton("确定", (dialog, which) -> {
                            })
                            .show();
                }
            }
        }
    }
}
